package com.aaa.doctor.service.impl;

import com.aaa.doctor.dao.CareHistoryDao;
import com.aaa.doctor.dao.CareOrderDao;
import com.aaa.doctor.dao.CareOrderItemDao;
import com.aaa.doctor.feign.DeptFegin;
import com.aaa.doctor.feign.UserFeign;
import com.aaa.doctor.service.CareHistoryService;
import com.aaa.doctor.utils.IdWorker;
import com.aaa.entity.*;
import com.aaa.utils.TokenUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
public class CareHistoryServiceImpl implements CareHistoryService {

    @Autowired
    private CareHistoryDao careHistoryDao;

    @Autowired
    private CareOrderItemDao careOrderItemDao;

    @Autowired
    private CareOrderDao careOrderDao;


    @Override
    public Result<List<CareHistory>> getCareHistoryByPatientId(String patientId) {
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        if (Objects.nonNull(patientId)) {
            wrapper.eq("patient_id", patientId);
            //查询病历表里所有信息
            List<CareHistory> careHistories = careHistoryDao.selectList(wrapper);
            //遍历
            for (CareHistory careHistory : careHistories) {
                QueryWrapper<CareOrder> wrapper1 = new QueryWrapper<>();
                if (Objects.nonNull(careHistory.getChId())) {
                    wrapper1.eq("ch_id", careHistory.getChId());
                    List<CareOrder> careOrders = careOrderDao.selectList(wrapper1);
                    careHistory.setCareOrderList(careOrders);
                    for (CareOrder careOrder : careOrders) {
                        QueryWrapper<CareOrderItem> wrapper2 = new QueryWrapper<>();
                        if (Objects.nonNull(careOrder.getCoId())) {
                            wrapper2.eq("co_id", careOrder.getCoId());
                            List<CareOrderItem> careOrderItems = careOrderItemDao.selectList(wrapper2);
                            careOrder.setCareOrderItemList(careOrderItems);
                        }
                    }
                }
            }
            return new Result<>(200, "查询就诊信息成功", careHistories);
        }
        return null;
    }

    /**
     * 根据挂号单ID查询对应的病历信息
     *
     * @param registrationId
     * @return
     */
    @Override
    public Result<List<CareHistory>> getCareHistoryByRegId(String registrationId) {
        List<CareHistory> careHistories = careHistoryDao.getCareHistoryByRegId(registrationId);
        return new Result<>(200, "查询病例信息成功", careHistories);
    }

    @Autowired
    private UserFeign userFeign;
    @Autowired
    private DeptFegin deptFegin;

    /**
     * 新开就诊 --- 保存病例
     * @param regId
     * @param careHistory
     * @return
     */
    @Override
    public Result saveCareHistory(String regId, CareHistory careHistory) {
        QueryWrapper<CareHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("reg_id",regId);
        //根据病例编号查询该患者病例是否存在
        CareHistory selectById = careHistoryDao.selectOne(wrapper);
        // 获取token
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        //获取token里的信息
        Map<String, Object> tokenInfo = TokenUtils.getInfo(token);
        // 在token里获取当前用户手机号信息
        String phone = tokenInfo.get("username").toString();
        // 获取手机号信息
        User user = userFeign.getByUsername(phone);
        careHistory.setUserId(user.getUserId());
        careHistory.setUserName(user.getUserName());
        careHistory.setDeptId(user.getDeptId());
        Long userId = user.getDeptId();
        //远程调用 根据部门ID查询部门名称
        Dept deptById = deptFegin.getAllDept(userId);
        //赋值
        careHistory.setDeptName(deptById.getDeptName());
        careHistory.setRegId(regId);
        //获取现在时间
        LocalDateTime dateTime = LocalDateTime.now();
        careHistory.setCareTime(dateTime);
        if (selectById == null) {
            //雪花算法 随机生成chId
            IdWorker worker = new IdWorker();//雪花算法
            long chId = worker.nextId();
            String newChId = "CH" + chId;
            //赋值
            careHistory.setChId(newChId);
            //插入数据
            int insert = careHistoryDao.insert(careHistory);
            if (insert > 0) {
                return new Result<>(200,"保存病例成功",newChId);
            }
            return new Result<>(200,"保存病例失败");
        } else {
            wrapper.eq("ch_id",selectById.getChId());
            int i = careHistoryDao.update(careHistory,wrapper);
            if (i > 0){
                return new Result<>(200,"修改病例成功",selectById.getChId());
            }
            return new Result<>(200,"修改病例失败");
        }
    }
}
